//
//  HNSH_GoodsLevelViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/15.
//

#import "HNSH_GoodsLevelViewController.h"

#import "HNSH_GoodsLevelTopView.h"
#import "HNSH_GoodsLevelCell.h"

@interface HNSH_GoodsLevelViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;
/* headerView */
@property (nonatomic, strong) HNSH_GoodsLevelTopView *headerView;
/* headerModel */
@property (nonatomic, strong) HNSH_MyTeamModel *headerModel;

@end

static NSString *HNSH_GoodsLevelCellID = @"HNSH_GoodsLevelCellID";

@implementation HNSH_GoodsLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"我的等级" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    
    [self configTableView];
    
    [self loadFarmTeamData];
}

- (void)configTableView {
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@""] title:@""];
    
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.tableView registerClass:[HNSH_GoodsLevelCell class] forCellReuseIdentifier:HNSH_GoodsLevelCellID];
    self.tableView.rowHeight = 93;
    self.tableView.estimatedRowHeight = 93;
}

#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_GoodsLevelCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_GoodsLevelCellID forIndexPath:indexPath];
    cell.model = self.listAry[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark networking
- (void)loadFarmTeamData {
    __weak typeof(self) weakSelf = self;
    [HNSH_UserNetServer requestUserTeamConsumeInfoSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_MyTeamModel *headerModel = [HNSH_MyTeamModel yy_modelWithDictionary:responseObject.resultset];
            self.headerModel = headerModel;
            weakSelf.headerView.frame = CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+160+headerModel.level_info.requirements.count*58+85);
            weakSelf.headerView.model = headerModel;
            weakSelf.tableView.tableHeaderView = self.headerView;
            [weakSelf.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
    
}

- (void)refreshData:(NSInteger)page {
    [HNSH_UserNetServer requestUserTeamConsumeFansListWithPage:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_MyTeamListModel class] json:responseObject.resultset[@"list"]];
            self.headerView.countString = [NSString stringWithFormat:@"%@",responseObject.resultset[@"total_count"]];
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            [self.listAry addObjectsFromArray:ary];
            [self.tableView reloadData];
            [self endRefresh:ary.count>=10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

- (HNSH_GoodsLevelTopView *)headerView {
    if (!_headerView) {
        _headerView = [[HNSH_GoodsLevelTopView alloc]init];
    }
    return _headerView;
}

@end
